package com.bawei.a2010douyin;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.functions.Consumer;

import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.tbruyelle.rxpermissions2.RxPermissions;

public class MapActivity extends AppCompatActivity {
    public LocationClient mLocationClient = null;
    private BDAbstractLocationListener myListener = new BDAbstractLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取地址相关的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

            String addr = location.getAddrStr();    //获取详细地址信息
            String country = location.getCountry();    //获取国家
            String province = location.getProvince();    //获取省份
            String city = location.getCity();    //获取城市
            String district = location.getDistrict();    //获取区县
            String street = location.getStreet();    //获取街道信息
            String adcode = location.getAdCode();    //获取adcode
            String town = location.getTown();    //获取乡镇信息
            Toast.makeText(MapActivity.this, ""+addr, Toast.LENGTH_SHORT).show();
        }
    };

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        //rxpermissions动态获取权限
        new RxPermissions(this)
                .request(  "android.permission.VIBRATE",
                        "android.permission.RECORD_AUDIO" ,
                        "android.permission.CAMERA",
                        "android.permission.ACCESS_NETWORK_STATE",
                        "android.permission.WRITE_EXTERNAL_STORAGE" ,
                        "android.permission.ACCESS_FINE_LOCATION",
                        "android.permission.GET_TASKS" ,
                        "android.permission.ACCESS_WIFI_STATE" ,
                        "android.permission.CHANGE_WIFI_STATE" ,
                        "android.permission.WAKE_LOCK",
                        "android.permission.MODIFY_AUDIO_SETTINGS",
                        "android.permission.READ_PHONE_STATE",
                        "android.permission.RECEIVE_BOOT_COMPLETED",
                        "android.permission.FOREGROUND_SERVICE",
                        "android.permission.ACCESS_COARSE_LOCATION")
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        //权限授予成功后再开启定位
                        mLocationClient.start();
                    }
                });

        mLocationClient = new LocationClient(getApplicationContext());
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        LocationClientOption option = new LocationClientOption();

        option.setIsNeedAddress(true);
//可选，是否需要地址信息，默认为不需要，即参数为false
//如果开发者需要获得当前点的地址信息，此处必须为true

        option.setNeedNewVersionRgc(true);
//可选，设置是否需要最新版本的地址信息。默认需要，即参数为true

        mLocationClient.setLocOption(option);
//mLocationClient为第二步初始化过的LocationClient对象
//需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
//更多LocationClientOption的配置，请参照类参考中LocationClientOption类的详细说明

    }
}